﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace CP.SharePoint.SiteStyles.ControlTemplates
{
    public partial class SetStyle : UserControl
    {
        /// <summary>
        /// Method executed when the page loads. Handles confirming that the necessary
        /// properties exist, converting the properties to their proper data type, and
        /// setting the site style for the current site.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SPWeb web = SPContext.Current.Web;

                if (web.Properties.ContainsKey(Resources.PropertyCurrentSiteStyle) &&
                    web.Properties.ContainsKey(Resources.PropertyCurrentSiteStyleCssPath) &&
                    web.Properties.ContainsKey(Resources.PropertyCurrentSiteStyleTheming))
                {
                    bool enableTheming;
                    string cssUrl = web.Properties[Resources.PropertyCurrentSiteStyleCssPath];
                    if (bool.TryParse(web.Properties[Resources.PropertyCurrentSiteStyleTheming], out enableTheming))
                    {
                        if (!string.IsNullOrEmpty(cssUrl))
                        {
                            SetSiteStyle(cssUrl, enableTheming);
                        }
                    }
                    else
                    {
                        SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("CP.SharePoint.SiteStyles - Control Template", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, string.Format("Error converting theming property '{0}' with value '{1}' to a boolean.", Resources.PropertyCurrentSiteStyleTheming, web.Properties[Resources.PropertyCurrentSiteStyleTheming]));
                    }
                }
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("CP.SharePoint.SiteStyles - Control Template", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
            }
        }

        /// <summary>
        /// Sets the site style for the current site based on the parameters. Uses
        /// SharePoint's CssRegistration class to handle including the CSS file in
        /// the page output.
        /// </summary>
        /// <param name="cssUrl">The URL to the CSS file for the currently selected
        /// site style. Should either be relative or begin with /_layouts.</param>
        /// <param name="enableTheming">Boolean value representing whether or not
        /// theme support should be enabled for this CSS file.</param>
        protected void SetSiteStyle(string cssUrl, bool enableTheming)
        {           
            // confirm this ends in css otherwise SharePoint will bomb
            if (cssUrl.EndsWith(".css"))
            {
                CssRegistration cssReg = new CssRegistration();
                cssReg.Name = SPUtility.GetServerRelativeUrlFromPrefixedUrl(cssUrl);
                cssReg.After = "corev4.css";
                cssReg.EnableCssTheming = enableTheming;

                stylePlaceHolder.Controls.Add(cssReg);
            }
        }
    }
}
